package it.luigiarpino.alt2jsim.gui;

import java.awt.BorderLayout;
import java.io.File;
import it.luigiarpino.alt2jsim.core.ExportToNeptuneXMLFormat;
import it.luigiarpino.alt2jsim.core.Grafo;
import it.luigiarpino.alt2jsim.utility.FormLayout;
import javax.swing.*;

public class GuiEsportaToNeptune extends JFrame
{
	
	private JCheckBox jCheckBoxBuildLink;
	private JTextField jTextFieldDescription;
    private JTextField jTextFieldBanda;
    private JTextField jTextFieldRitardo;
    private JTextField jTextFieldLoss;
    private JTextField jTextFieldDuplicated;
    private JTextField jTextFieldCorrupt;
    
    private JTextField jTextFieldRam;
    private JTextField jTextFieldOsName;
    private JTextField jTextFieldPassword;
    private JTextField jTextFieldTemplate;
    
    private JButton jButtonInvia;
    private JButton jButtonAnnulla;
    

    
	private Grafo grafo;

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	public GuiEsportaToNeptune(Grafo g)
	{
		try{ 
            javax.swing.UIManager.setLookAndFeel(javax.swing.UIManager.getSystemLookAndFeelClassName());     
    	} 
    	catch(Exception e) { }
    	this.setTitle("Export to Neptune XML file format");
    	this.grafo=g;
	    JPanel formPanel = new JPanel(new FormLayout());
	   
	    // Riga vuota
	    formPanel.add(new JLabel(" "));
	    formPanel.add(new JLabel(" "));
	    
	    this.jTextFieldDescription=new JTextField(30);
	    this.jTextFieldDescription.setText("");
	    formPanel.add(new JLabel("Topology description"));
	    formPanel.add(this.jTextFieldDescription);
	    
	    // Riga vuota
	    formPanel.add(new JLabel(" "));
	    formPanel.add(new JLabel(" "));
	    
	    formPanel.add(new JLabel("<html><b>Link's properties</b></html>"));
	    formPanel.add(new JLabel(" "));
	   
	    this.jCheckBoxBuildLink=new JCheckBox("",true);
	    this.jCheckBoxBuildLink.addItemListener(new java.awt.event.ItemListener() {
	        public void itemStateChanged(java.awt.event.ItemEvent evt) {
	            BuildLink(evt);
	        }
	    });
	    formPanel.add(new JLabel("Delay proportional to the link's length"));
	    formPanel.add(this.jCheckBoxBuildLink);
	    
	    
	   
	    
	    this.jTextFieldRitardo=new JTextField(10);
	    this.jTextFieldRitardo.setText("1000");
	    formPanel.add(new JLabel("Max link delay [ms]"));
	    formPanel.add(this.jTextFieldRitardo);
	    
	    this.jTextFieldBanda=new JTextField(10);
	    this.jTextFieldBanda.setText("1024");
	    formPanel.add(new JLabel("Bandwidth [Kbps]"));
	    formPanel.add(this.jTextFieldBanda);
	    
	    this.jTextFieldLoss=new JTextField(5);
	    this.jTextFieldLoss.setText("0.0");
	    formPanel.add(new JLabel("Packet loss [%]"));
	    formPanel.add(this.jTextFieldLoss);
	    
	    this.jTextFieldDuplicated=new JTextField(5);
	    this.jTextFieldDuplicated.setText("0.0");
	    formPanel.add(new JLabel("Packet duplicated [%]"));
	    formPanel.add(this.jTextFieldDuplicated);
	    
	    this.jTextFieldCorrupt=new JTextField(5);
	    this.jTextFieldCorrupt.setText("0.0");
	    formPanel.add(new JLabel("Packet corrupted [%]"));
	    formPanel.add(this.jTextFieldCorrupt);
	    
	    // riga vuota
	    formPanel.add(new JLabel(" "));
	    formPanel.add(new JLabel(" "));
	    
	    formPanel.add(new JLabel("<html><b>Node's properties</b></html>"));
	    formPanel.add(new JLabel(" "));
	    
	    this.jTextFieldOsName=new JTextField(12);
	    this.jTextFieldOsName.setText("CentOS");
	    formPanel.add(new JLabel("Os type"));
	    formPanel.add(this.jTextFieldOsName);
	    
	    this.jTextFieldPassword=new JTextField(12);
	    this.jTextFieldPassword.setText("antoxps");
	    formPanel.add(new JLabel("Password"));
	    formPanel.add(this.jTextFieldPassword);
	    
	    this.jTextFieldTemplate=new JTextField(12);
	    this.jTextFieldTemplate.setText("CentOS_5_2");
	    formPanel.add(new JLabel("Template name"));
	    formPanel.add(this.jTextFieldTemplate);
	    
	    this.jTextFieldRam=new JTextField(5);
	    this.jTextFieldRam.setText("256");
	    formPanel.add(new JLabel("RAM [MBs]"));
	    formPanel.add(this.jTextFieldRam);
	    
	    formPanel.add(new JLabel(" "));
	    formPanel.add(new JLabel(" "));
	    
	    // Costruisco i Bottoni
	    this.jButtonInvia=new JButton("Export");
	    this.jButtonInvia.addActionListener(new java.awt.event.ActionListener() {
	        public void actionPerformed(java.awt.event.ActionEvent evt) {
	            jButtonInviaActionPerformed(evt);
	        }
	    });
	    this.jButtonAnnulla=new JButton("Cancel");
	    this.jButtonAnnulla.addActionListener(new java.awt.event.ActionListener() {
	        public void actionPerformed(java.awt.event.ActionEvent evt) {
	            jButtonAnnullaActionPerformed(evt);
	        }
	    });
	  
	    formPanel.add(this.jButtonInvia);
	    formPanel.add(this.jButtonAnnulla);
	    this.getContentPane().add(formPanel ,BorderLayout.CENTER);
	    this.pack();
	}

	private void BuildLink(java.awt.event.ItemEvent evt) 
	{
		/*
		if (evt.getStateChange() == java.awt.event.ItemEvent.SELECTED){
	     
	        this.jTextFieldRitardo.setEnabled(true);
		}
	    else
	    {
	        this.jTextFieldRitardo.setEnabled(false);
	    }
	    */
	}


	private void jButtonInviaActionPerformed(java.awt.event.ActionEvent evt) 
	{
		String descTopologia=this.jTextFieldDescription.getText();
		String ram=this.jTextFieldRam.getText();
		//TODO prevedere la possibilità per 
		// l'utente di scegliere l'interfaceBindings
		String interfaceBindings="bridge";
		String osName=this.jTextFieldOsName.getText();
		String osPassword=this.jTextFieldPassword.getText();
		String template=this.jTextFieldTemplate.getText();
		Long banda;
		Long ritardo;
		boolean evaluateLinkDelay=this.jCheckBoxBuildLink.isSelected();
		Double loss;
		Double duplicated;
		Double corrupted;
	    try{
	        ritardo=new Long(this.jTextFieldRitardo.getText());
	    }
	    catch(java.lang.Exception e){
	        JOptionPane.showMessageDialog(null,"Tempo di Ritardo non valido","Attenzione",JOptionPane.ERROR_MESSAGE);
	        return;
	    }
	    try
	    {
	        banda=new Long(this.jTextFieldBanda.getText());
	    }
	    catch(java.lang.Exception e)
	    {
	        JOptionPane.showMessageDialog(null,"Larghezza di banda non valida","Attenzione",JOptionPane.ERROR_MESSAGE);
	        return;
	    }
	    try
	    {
	        loss = new Double(this.jTextFieldLoss.getText());
	    }
	    catch(java.lang.Exception e)
	    {
	        JOptionPane.showMessageDialog(null,"Packet loss not valid!","Error", JOptionPane.ERROR_MESSAGE);
	        return;
	    }
	    try
	    {
	        duplicated = new Double(this.jTextFieldDuplicated.getText());
	    }
	    catch(java.lang.Exception e)
	    {
	        JOptionPane.showMessageDialog(null,"Packet duplicated not valid!","Error", JOptionPane.ERROR_MESSAGE);
	        return;
	    }
	    try
	    {
	    	corrupted = new Double(this.jTextFieldCorrupt.getText());
	    }
	    catch(java.lang.Exception e)
	    {
	        JOptionPane.showMessageDialog(null,"Packet corrupted not valid!","Error", JOptionPane.ERROR_MESSAGE);
	        return;
	    }
	    JFileChooser f=new JFileChooser();
	    f.showDialog(new JFrame(),"Export to Neptune XML file format");  
	    File esportFile=f.getSelectedFile();
	    if (esportFile!=null)
	    {		
	    	ExportToNeptuneXMLFormat write=new ExportToNeptuneXMLFormat(this.grafo);
	        write.scriviFile( esportFile.getAbsolutePath(),descTopologia, ram, interfaceBindings,
	    			osName, osPassword, template, ritardo, evaluateLinkDelay, banda, loss, duplicated, corrupted );
	        this.setVisible(false);
	        this.dispose();
	    }
	}
	
	
	private void jButtonAnnullaActionPerformed(java.awt.event.ActionEvent evt) {
	    this.setVisible(false);
	    this.dispose();
	}
	
	
	public static void main(String[] arg)
	{
	    GuiEsportaToNeptune gui = new GuiEsportaToNeptune(null);
	    gui.setLocationRelativeTo(null);
	    gui.setVisible(true);
	}

}
